package com.leoandroid.tool.toolsbox.largeread.fragment

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.leoandroid.tool.toolsbox.camera.RecognizeCameraActivity
import com.leoandroid.tool.toolsbox.camera.utils.Constants
import com.leoandroid.tool.toolsbox.largeread.activity.RulerActivity
import com.leoandroid.tool.toolsbox.largeread.activity.SurfaceCameraActivity
import com.leoandroid.tool.toolsbox.largeread.databinding.FragmentToolBinding
import com.leoandroid.tool.toolsbox.largeread.utils.FlashlightUtils
import com.leoandroid.tool.toolsbox.largeread.utils.SPUtil

private const val requestCode_Camera = 1

class ToolFragment : BaseFragment() {

    companion object {
        fun getInstance(): ToolFragment {
            return ToolFragment()
        }
    }

    var bindingF: FragmentToolBinding? = null
    val binding get() = bindingF!!
    var cameraPer = arrayOf(Manifest.permission.CAMERA)
    var storagePer = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    private var isLighOn = false


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        bindingF = FragmentToolBinding.inflate(inflater, container, false)

        // 放大镜
        binding.middleFunc2.setOnClickListener {
            val cameraDenied = SPUtil.with(activity).load().read("CAMERA_DENIED", false)
            if (cameraDenied) {
                Toast.makeText(activity, "没有相机授权，功能使用受限,请先授权", Toast.LENGTH_SHORT)
                    .show()
                return@setOnClickListener
            }
            val camera = SPUtil.with(activity).load().read("CAMERA", false)
            if (!camera) {
                //申请相机权限
                activity?.let { it1 ->
                    ActivityCompat.requestPermissions(
                        it1,
                        cameraPer,
                        requestCode_Camera
                    )
                }
                return@setOnClickListener
            }
            jumpCamera()
        }

        // 动物识别
        binding.middleFunc1.setOnClickListener {
            val intent = Intent(context, RecognizeCameraActivity::class.java).also {
                it.putExtra(Constants.RECOGNIZE_TYPE, Constants.RECOGNIZE_TYPE_ANIMAL)
            }
            startActivity(intent)
        }

        // 尺子测量
        binding.middleFunc3.setOnClickListener {
            val intent = Intent(context, RulerActivity::class.java)
            startActivity(intent)
        }

        // 手电
        binding.middleFunc4.setOnClickListener {
            if (!isLighOn) {
                turnOnFlash()
            } else {
                turnOffFlash()
            }
        }

        // 字体大小设置
        binding.middleFunc5.setOnClickListener {
            val intent = Intent(Settings.ACTION_DISPLAY_SETTINGS)
            startActivity(intent)
        }
        return binding.root
    }

    fun jumpCamera() {
        val intent = Intent(context, SurfaceCameraActivity::class.java)
        startActivity(intent)
    }

    private fun turnOnFlash() {
        FlashlightUtils.linghtOn()
        isLighOn = true
    }

    // Turning Off flash
    private fun turnOffFlash() {
        FlashlightUtils.linghtOff()
        isLighOn = false
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
    }

    override fun initView(view: View) {

    }
}